const {getArrangeByUsername, newArrange, updateArrange} = require("../controller/arrange");
const {updateCoachArrange} = require("../controller/coach");
const Response = require('../common/Response')
const router = require('koa-router')()

router.prefix('/api/arrange')

// 未用到
router.post('/detail', async (context, next) => {
  const row = await getArrangeByUsername(context.session.username)
  context.body = Response.success(row)
})

router.post('/new', async (context, next) => {
  // 修改教练练车时间安排
  const val = await newArrange(context.request.body)
  if (val) {
    const CoachVal = await updateCoachArrange(context.session.username)
    if (CoachVal) {
      context.body = Response.success('练车时间安排成功')
    }
  } else {
    context.body = Response.failure('练车时间安排失败')
  }
})

router.post('/update', async (context, next) => {
  const val = await updateArrange(context.request.body)
  if (val) {
    context.body = Response.success('修改练车时间安排成功')
  } else {
    context.body = Response.failure('修改练车时间安排失败')
  }
})

module.exports = router
